<?php

use console\db\Migration;

/**
 * 用户相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091356_t_user_auth extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 用户认证信息表
         * @table `user_authent`.
         */
        $this->tableBuilder([
            'name' => 'user_authent',
            'comment' => '用户认证信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'realname' => $this->string(50)->notNull()->comment('真实姓名'),
                'gender' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('性别:0未知，1男，2女'),
                'ict_id' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('证件类型'),
                'idcardno' => $this->char(32)->notNull()->unique()->comment('加密证件号'),
                'idcardno_hash' => $this->char(255)->notNull()->comment('加密证件号（可逆）'),
                'photo' => $this->char(255)->notNull()->comment('人像照'),
                'passport_front' => $this->char(255)->notNull()->comment('证件照正面'),
                'passport_reverse' => $this->char(255)->notNull()->comment('证件照反面'),
                'expiry_date' => $this->date()->comment('有效期'),
                'birthday' => $this->date()->comment('出生日期'),
                'audit_at' => $this->unixTimestamp()->comment('认证通过时间'),
                'mark' => $this->string(255)->comment('审核备注'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('认证状态'),
                'created_at' => $this->unixTimestamp()->comment('提交认证时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                    'unique' => true,
                ]
            ]
        ]);
        
        
        /**
         * 用户认证审核日志
         * @table `user_authent_log`.
         */
        $this->tableBuilder([
            'name' => 'user_authent_log',
            'comment' => '用户认证审核日志',
            'columns' => [
                'id' => $this->primaryKey(),
                'admin_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('管理员ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'authent_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('认证ID'),
                'mark' => $this->string(255)->comment('审核备注'),
                'created_at' => $this->unixTimestamp()->comment('提交认证时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);

        /**
         * 用户收件地址表
         * @todo create table `user_address`.
         */
        $this->tableBuilder([
            'name' => 'user_address',
            'comment' => '用户收件地址表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'name' => $this->string(50)->notNull()->comment('收件人姓名'),
                'mobile' => $this->char(11)->notNull()->comment('手机号'),
                'province' => $this->string(50)->notNull()->comment('省份'),
                'city' => $this->string(50)->notNull()->comment('市'),
                'district' => $this->string(50)->notNull()->comment('地区'),
                'address_detail' => $this->string(255)->notNull()->comment('详细地址'),
                'area_code' => $this->char(10)->comment('地区码'),
                'postal_code' => $this->char(6)->comment('邮编'),
                'is_default' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('是否默认地址'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id'
                ]
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%user_address}}');
        $this->dropTable('{{%user_authent_log}}');
        $this->dropTable('{{%user_authent}}');
    }

}
